/*
 * Copyright 2011 yura.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.opu.queryhandler.n.client.ui.actions;

import java.awt.event.ActionEvent;
import java.util.Locale;
import javax.swing.ButtonGroup;
import javax.swing.JMenu;
import javax.swing.JMenuItem;
import javax.swing.JRadioButtonMenuItem;
import javax.swing.SwingUtilities;
import javax.swing.UIManager;
import javax.swing.UIManager.LookAndFeelInfo;
import org.opu.queryhandler.n.client.ui.MainFrame;
import org.opu.queryhandler.n.util.Logger;

/**
 *
 */
public abstract class LafAction extends AbstractAction {

    public static JMenu getLaFMenu(final MainFrame mainView, String name) {
        JMenu ret = new JMenu(name);
        ButtonGroup group = new ButtonGroup();
        final Logger lLogger = Logger.getInstance();
        
        for (final LookAndFeelInfo info : UIManager.getInstalledLookAndFeels()) {

            final String lafNm = info.getClassName();
            
            // create action
            JMenuItem add = ret.add(new JRadioButtonMenuItem(new LafAction() {

                @Override
                public String getName() {
                    return info.getName();
                }

                @Override
                public void action(ActionEvent event) {
                    try {
                        UIManager.setLookAndFeel(lafNm);
                        SwingUtilities.updateComponentTreeUI(mainView);
                        mainView.storeLookAndFeel(lafNm);
                    } catch (Exception e) {
                        lLogger.error(this, "Can't set l&f: " + lafNm);
                        lLogger.error(this, e);
                    }
                }
            }));

            // add to group
            group.add(add);

            // try to set default selected value
            try {
                if (UIManager.getLookAndFeel().getClass() == Class.forName(lafNm)) {
                    add.setSelected(true);
                    lLogger.info(LafAction.class, "Chose l&f - " + lafNm);
                } else {
                    lLogger.debug(LafAction.class, "Don't chose l&f - " + lafNm);
                }
            } catch (ClassNotFoundException ex) {
                lLogger.error(LafAction.class, "Can't get class: " + ex);
                lLogger.error(LafAction.class, ex);
            }
            
            
        }

        return ret;
    }
}
